package br.com.center.loja.auth;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

import org.simpleframework.xml.core.Persister;

import br.com.center.loja.model.Permission;

public class SecurityLoader {
	
	public static Security load(String file) throws Exception{
		FileInputStream in = new FileInputStream(file);
		BufferedInputStream input = new BufferedInputStream(in);
		
		return load(input);
	}
	
	public static Security load(InputStream input) throws Exception{
		if (input == null){
			throw new NullPointerException();
		}
		
		return new Persister()
					.read(Security.class, input);
	}
	
	public static void main(String[] args) throws Exception {
		Security load = SecurityLoader.load("D:\\academia java\\workspace\\eloja\\WebContent\\WEB-INF\\security.xml").load();
		
		System.out.println(load.getRules().size());
		
		for(Rule rule : load.getRules()){
			System.out.println(rule.getPermissions().size());
			
			for(Permission permission : 
				rule.getPermissions()){
				System.out.println(permission.getCode());
				System.out.println(permission.getName());
				System.out.println(permission.getDescription());
				System.out.println();
			}
		}
	}

}
